library(leaflet)
library(dplyr)
library(geojsonio)
library(htmltools)
library(RColorBrewer)

1: read file in

nycounties <- geojson_read("../data/nyczip.geojson", what = "sp")

sales data

sales <- read.csv("../data/Sale_Data.csv", header=TRUE, stringsAsFactors = FALSE)

prepare

add sales to nycounty. for later use in sales heat map

nycounties$postalCode <- as.numeric(as.character(nycounties$postalCode))
dat <- nycounties@data
salesPrice <- NULL
for(i in 1:nrow(dat)){
  zip <- dat$postalCode[i]
  salesPrice <- c(salesPrice, sales$Price_Predict[sales$RegionName == zip])
}
nycounties@data$sales <- salesPrice

rental data

rental <- read.csv("../data/Rental_Data.csv", header=TRUE, stringsAsFactors = FALSE)

get 5 data sets for rentals and dump them

#for(i in 1:5){
#  bed <- filter(rental, Bedroom == i)
#  write.csv(bed, paste("~/Desktop/ADS/Fall2017-project2-fall2017-project2-grp1/data/rental_bed", i,".csv", sep = ""))
#}

add rentals to nycounty

dat <- nycounties@data
l <- list()
for (i in 1:5){
  s<-read.csv(paste("../data/rental_bed",i,".csv", sep=""), header=TRUE, stringsAsFactors = FALSE)
 # s$RegionName <- as.character(s$RegionName)
  
  rentalPrice <- NULL
  
  for(j in 1:nrow(dat)){
    zip <- dat$postalCode[j]
    rentalPrice <- c(rentalPrice, s$Price_Predict[s$RegionName == zip])
  }
  
  l[[i]] <- rentalPrice
}
nycounties@data$rental_Bed1 <- l[[1]]
nycounties@data$rental_Bed2 <- l[[2]]
nycounties@data$rental_Bed3 <- l[[3]]
nycounties@data$rental_Bed4 <- l[[4]]
nycounties@data$rental_Bed5 <- l[[5]]

hospital data

hospital <- read.csv("../data/Hospital_Data.csv", header=TRUE, stringsAsFactors = FALSE)
#hospital$Longitude <- hospital$Longitude*(-1) # correct the longitugde

theatre data

theatre <- read.csv("../data/Theatre_Data.csv", header=TRUE, stringsAsFactors = FALSE)

# coord <- sapply(theatre$the_geom, function(x) {
#   substr(x, 8, nchar(x)-1)
# }) %>% as.character()
# values <- strsplit(coord, split=" ") %>% unlist()
# theatre_long <- NULL
# theatre_lat <- NULL
# for(i in 1:(length(values)/2)){
#   theatre_long <- c(theatre_long,values[i*2-1])
#   theatre_lat <- c(theatre_lat, values[i*2])
# }
# theatre_long <- theatre_long %>% as.numeric()
# theatre_lat <- theatre_lat %>% as.numeric()
# theatre$longitude <- theatre_long
# theatre$latitude <- theatre_lat

subway data

subway <- read.csv("../data/Subway_Data.csv", header=TRUE, stringsAsFactors = FALSE)

crime data

crime <- read.csv("../data/Crime_Data.csv", header=TRUE, stringsAsFactors = FALSE)
crime <- crime %>% na.omit() 

2 make the base map color = “#444444”

basemap <- leaflet(nycounties) %>% addPolygons( color = "#BFA",weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, highlightOptions = highlightOptions(color = "red", weight = 2, bringToFront = TRUE)) %>% addTiles()

Icon Maker

IconMaker <- function(address){
      icon <- makeIcon(
      iconUrl = address,
      iconWidth = 30, iconHeight = 30,
      iconAnchorX = 0, iconAnchorY = 0)
}


hospitalIcon <- IconMaker("../fig/Hospital.png")
galleryIcon <- IconMaker("../fig/Gallery.png")
theatreIcon <- IconMaker("../fig/Theatre.png")
subwayIcon <- IconMaker("../fig/Subway.png")
crimeIcon <- IconMaker("../fig/Crime.png")

Hospital map

pops_hospital <- paste0("<center style='color:black'><strong style='color:blue'>",hospital$Facility.Name,"</strong>","<br/>",hospital$Phone, "<br/>", hospital$address, "</center>")

basemap %>% addMarkers(lng = hospital$Longitude, lat = hospital$Latitude, clusterOptions = markerClusterOptions() , popup = pops_hospital, icon=hospitalIcon)

Art map

pops_art <- paste0("<center style='color:black'><strong style='color:blue'>",art$NAME,"</strong>","<br/>",paste(art$ADDRESS1, art$ADDRESS2), "</center>")

basemap %>% addMarkers(lng = art$long, lat = art$lat,clusterOptions = markerClusterOptions() , popup = pops_art, icon=galleryIcon)

Theatre map

pops_theatre <- paste0("<center style='color:black'><strong style='color:blue'>",theatre$NAME,"</strong>","<br/>",paste(theatre$ADDRESS1, theatre$ADDRESS2), "</center>")
basemap %>% addMarkers(lng = theatre$long, lat = theatre$lat,clusterOptions = markerClusterOptions() , popup = pops_theatre, icon=theatreIcon)

Subway map

pops_subway <- paste0("<center style='color:black'><strong style='color:blue'>",subway$NAME,"</strong>","<br/>",subway$LINE, "</center>")
basemap %>% addMarkers(lng = subway$Long, lat = subway$Lat,clusterOptions = markerClusterOptions(), popup = pops_subway, icon=subwayIcon)

Crime map

pops_crime <- paste0("<center style='color:black'><strong style='color:blue'>",crime$LAW_CAT_CD,"</strong>","<br/>",crime$BORO_NM, "</center>")
basemap %>% addMarkers(lng = crime$Longitude, lat = crime$Latitude,clusterOptions = markerClusterOptions(), popup = pops_crime, icon=crimeIcon)

Sales map

bins <- c(0, 500, 1000, 1500, 2000, 2500,Inf)
#    YlGnBu (YlOrBr) (YlGn) Blues Reds (Accent) Oranges Purples
pal <- colorBin("YlOrRd", domain = nycounties$sales, bins = bins)
labels_sales <- sprintf(
  "<strong>ZipCode: %s</strong><br/>%g $ / ft<sup>2</sup>",
  as.character(nycounties$postalCode), nycounties$sales
) %>% lapply(htmltools::HTML)

basemap %>% addPolygons(
  fillColor = pal(nycounties$sales),
  color = "#444444", 
  weight = 2, 
  smoothFactor = 0.5, 
  opacity = 1.0, 
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 5,
    color = "#355C7D",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels_sales,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto")
  )  %>% addLegend(pal = pal, 
            values = nycounties$sales,
            opacity = 0.7, 
            title = 'Price Per Square Feet',
            position = "bottomright")

heap map funtion for rental

# rent_data : data set which consists of 5 types of bedrooms
# bed: type of bedroom such as 1,2,3,4,5 

bin1 <- c(0, 1000, 2000, 3000, 4000,Inf) #bed 1
bin2 <- c(0,1000,2000,3000,4000,5000,6000,7000, Inf) #bed2
bin3 <- c(0,2000,4000,6000,8000,Inf) #bed3
bin4 <- c(0,2000,4000,6000,8000,10000,Inf) #bed4
bin5 <- c(0,2000,4000,6000,8000,10000,Inf) #bed5


#YlGnBu (YlOrBr) (YlGn) Blues Reds (Accent) Oranges Purples
pal1 <- colorBin("YlGnBu", domain = nycounties$rental_Bed1, bins = bin1)
pal2 <- colorBin("Blues", domain = nycounties$rental_Bed2, bins = bin2)
pal3 <- colorBin("Reds", domain = nycounties$rental_Bed3, bins = bin3)
pal4 <- colorBin("Oranges", domain = nycounties$rental_Bed4, bins = bin4)
pal5 <- colorBin("Purples", domain = nycounties$rental_Bed5, bins = bin5)

rental bed-1 heat map

labels_bed1 <- sprintf(
  "<strong>ZipCode: %s</strong><br/>$%g",
  as.character(nycounties$postalCode), nycounties$rental_Bed1
) %>% lapply(htmltools::HTML)


basemap %>% addPolygons(
    fillColor = pal1(nycounties$rental_Bed1),
    color = "#444444", 
    weight = 2, 
    smoothFactor = 0.5, 
    opacity = 1.0, 
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#355C7D",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels_bed1,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
    )  %>% addLegend(pal = pal1, 
            values = nycounties$rental_Bed1,
            opacity = 0.7, 
            title = 'Rental Price For 1 Bedroom',
            position = "bottomright")

rental bed-2 heat map

labels_bed2 <- sprintf(
  "<strong>ZipCode: %s</strong><br/>$%g",
  as.character(nycounties$postalCode), nycounties$rental_Bed2
) %>% lapply(htmltools::HTML)


basemap %>% addPolygons(
    fillColor = pal2(nycounties$rental_Bed2),
    color = "#444444", 
    weight = 2, 
    smoothFactor = 0.5, 
    opacity = 1.0, 
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#355C7D",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels_bed2,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
    )  %>% addLegend(pal = pal2, 
            values = nycounties$rental_Bed2,
            opacity = 0.7, 
            title = 'Rental Price For 2 Bedrooms',
            position = "bottomright")

rental bed-3 heat map

labels_bed3 <- sprintf(
  "<strong>ZipCode: %s</strong><br/>$%g",
  as.character(nycounties$postalCode), nycounties$rental_Bed3
) %>% lapply(htmltools::HTML)


basemap %>% addPolygons(
    fillColor = pal3(nycounties$rental_Bed3),
    color = "#444444", 
    weight = 2, 
    smoothFactor = 0.5, 
    opacity = 1.0, 
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#355C7D",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels_bed3,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
    )  %>% addLegend(pal = pal3, 
            values = nycounties$rental_Bed3,
            opacity = 0.7, 
            title = 'Rental Price For 3 Bedrooms',
            position = "bottomright")

rental bed-4 heat map

labels_bed4 <- sprintf(
  "<strong>ZipCode: %s</strong><br/>$%g",
  as.character(nycounties$postalCode), nycounties$rental_Bed4
) %>% lapply(htmltools::HTML)


basemap %>% addPolygons(
    fillColor = pal4(nycounties$rental_Bed4),
    color = "#444444", 
    weight = 2, 
    smoothFactor = 0.5, 
    opacity = 1.0, 
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#355C7D",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels_bed4,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
    )  %>% addLegend(pal = pal4, 
            values = nycounties$rental_Bed4,
            opacity = 0.7, 
            title = 'Rental Price For 4 Bedrooms',
            position = "bottomright")

rental bed-5 heat map

labels_bed5 <- sprintf(
  "<strong>ZipCode: %s</strong><br/>$%g",
  as.character(nycounties$postalCode), nycounties$rental_Bed5
) %>% lapply(htmltools::HTML)


basemap %>% addPolygons(
    fillColor = pal5(nycounties$rental_Bed5),
    color = "#444444", 
    weight = 2, 
    smoothFactor = 0.5, 
    opacity = 1.0, 
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#355C7D",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels_bed5,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")
    )  %>% addLegend(pal = pal5, 
            values = nycounties$rental_Bed5,
            opacity = 0.7, 
            title = 'Rental Price For 5 Bedrooms',
            position = "bottomright")